package ActorObj;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class GameBall extends Actor {

	private TextureRegion region;
	private int radius=0;
	private Color color;
	public float angle=0;
	public float speed=2f;
	public boolean isOriginator=false;
	
	public GameBall(int radius,float r,float g,float b,float a) {
		this.radius=radius;
		this.color=new Color(r/255f, g/255f, b/255f, 1); 
		setTextureRegion();
		super.size(radius);
	}
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		// TODO Auto-generated method stub
		batch.draw(region, getX()-(float)radius, getY()-(float)radius);
		
	}
	
	@Override
	public void setSize(float width, float height) {
		// TODO Auto-generated method stub
		int newRadius=Math.round(width);
		if(newRadius!=radius)
		{
			radius=newRadius;
			setTextureRegion();
			super.size(radius);
		}
	}
	
	public float getRadius() {
		return radius;
	}
	
	private int getPixmapSize() {
		int psize=2;
		while (true) {
			if(psize>=radius*2)
			{
				return psize;
			}
				
			psize*=psize;
		}
	}
	
	private void setTextureRegion() {
		int psize=getPixmapSize();
		Pixmap backgrounPixmap= new Pixmap(psize, psize, Format.RGBA8888);
		backgrounPixmap.setColor(color);
		backgrounPixmap.fillCircle(radius, radius, radius);
		Texture texture=new Texture(backgrounPixmap);
		region=new TextureRegion(texture,radius*2+1,radius*2+1);
	}
	//------------------------------
	public void move()
	{
		float radian=(float) (Math.PI*angle/180);
		float dx=((float)Math.cos(radian))*speed;
		float dy=((float)Math.sin(radian))*speed;
		float cx=getX();
		float cy=getY();
		float lx=dx+cx;
		float ly=dy+cy;
		setX( lx);
		setY( ly);
	}
}
